/*********************\
* Author:Dzlua
* QQ:505544956
* Time:2017/04/15
\**********************/
#include "LuaContainer.h"

#define CObjPtr ((CContainerUI*)m_cobj)

namespace DuiLib {

int LuaContainer::GetItemAt(LuaState *L)
{
    LuaStack args(L);
    int iIndex = LuaHelperNS::GetInt(args[2], -1);
    CControlUI* ctrl = CObjPtr->GetItemAt(iIndex);
    LuaHelperNS::PushCObj(L, ctrl);
    return 1;
}

int LuaContainer::GetItemIndex(LuaState *L)
{
    LuaStack args(L);
    CControlUI* ctrl = (CControlUI*)LuaHelperNS::GetCObjectData(args[2]);
    int iIndex = CObjPtr->GetItemIndex(ctrl);
    L->PushInteger(iIndex);
    return 1;
}

int LuaContainer::SetItemIndex(LuaState *L)
{
    LuaStack args(L);
    CControlUI* ctrl = (CControlUI*)LuaHelperNS::GetCObjectData(args[2]);
    int iIndex = LuaHelperNS::GetInt(args[3], -1);
    bool bRet = CObjPtr->SetItemIndex(ctrl, iIndex);
    L->PushBoolean(bRet);
    return 1;
}

int LuaContainer::GetCount(LuaState *L)
{
    L->PushInteger(CObjPtr->GetCount());
    return 1;
}

int LuaContainer::Add(LuaState *L)
{
    LuaStack args(L);
    CControlUI* ctrl = (CControlUI*)LuaHelperNS::GetCObjectData(args[2]);
    bool bRet = CObjPtr->Add(ctrl);
    L->PushBoolean(bRet);
    return 1;
}

int LuaContainer::AddAt(LuaState *L)
{
    LuaStack args(L);
    CControlUI* ctrl = (CControlUI*)LuaHelperNS::GetCObjectData(args[2]);
    int iIndex = LuaHelperNS::GetInt(args[3], -1);
    bool bRet = CObjPtr->AddAt(ctrl, iIndex);
    L->PushBoolean(bRet);
    return 1;
}

int LuaContainer::Remove(LuaState *L)
{
    LuaStack args(L);
    CControlUI* ctrl = (CControlUI*)LuaHelperNS::GetCObjectData(args[2]);
    bool bRet = CObjPtr->Remove(ctrl);
    L->PushBoolean(bRet);
    return 1;
}

bool LuaContainer::RemoveAt(int iIndex)
{
    return CObjPtr->RemoveAt(iIndex);
}

void LuaContainer::RemoveAll()
{
    CObjPtr->RemoveAll();
}

void LuaContainer::SetVisible(bool bVisible)
{
    CObjPtr->SetVisible(bVisible);
}

void LuaContainer::SetInternVisible(bool bVisible)
{
    CObjPtr->SetInternVisible(bVisible);
}

void LuaContainer::SetEnabled(bool bEnabled)
{
    CObjPtr->SetEnabled(bEnabled);
}
void LuaContainer::SetMouseEnabled(bool bEnable)
{
    CObjPtr->SetMouseEnabled(bEnable);
}

int LuaContainer::GetInset(LuaState *L)
{
    RECT rc = CObjPtr->GetInset();
    LuaHelperNS::PushRect(L, rc);
    return 1;
}

int LuaContainer::SetInset(LuaState *L)
{
    LuaStack args(L);
    RECT rc = LuaHelperNS::GetRect(args[2]);
    CObjPtr->SetInset(rc);
    return 0;
}

int LuaContainer::GetChildPadding()
{
    return CObjPtr->GetChildPadding();
}

void LuaContainer::SetChildPadding(int iPadding)
{
    CObjPtr->SetChildPadding(iPadding);
}

UINT LuaContainer::GetChildAlign()
{
    return CObjPtr->GetChildAlign();
}

void LuaContainer::SetChildAlign(UINT iAlign)
{
    CObjPtr->SetChildAlign(iAlign);
}

UINT LuaContainer::GetChildVAlign()
{
    return CObjPtr->GetChildVAlign();
}

void LuaContainer::SetChildVAlign(UINT iVAlign)
{
    CObjPtr->SetChildVAlign(iVAlign);
}

bool LuaContainer::IsAutoDestroy()
{
    return CObjPtr->IsAutoDestroy();
}

void LuaContainer::SetAutoDestroy(bool bAuto)
{
    CObjPtr->SetAutoDestroy(bAuto);
}

bool LuaContainer::IsDelayedDestroy()
{
    return CObjPtr->IsDelayedDestroy();
}

void LuaContainer::SetDelayedDestroy(bool bDelayed)
{
    CObjPtr->SetDelayedDestroy(bDelayed);
}

bool LuaContainer::IsMouseChildEnabled()
{
    return CObjPtr->IsMouseChildEnabled();
}

void LuaContainer::SetMouseChildEnabled(bool bEnable)
{
    CObjPtr->SetMouseChildEnabled(bEnable);
}

int LuaContainer::FindSelectable(LuaState *L)
{
    LuaStack args(L);
    int iIndex = LuaHelperNS::GetInt(args[2], -1);
    bool bForward = LuaHelperNS::GetBool(args[3], true);
    int iRet = CObjPtr->FindSelectable(iIndex, bForward);
    L->PushInteger(iRet);
    return 1;
}

int LuaContainer::GetClientPos(LuaState *L)
{
    RECT rc = CObjPtr->GetClientPos();
    LuaHelperNS::PushRect(L, rc);
    return 1;
}

int LuaContainer::SetPos(LuaState *L)
{
    LuaStack args(L);
    RECT rc = LuaHelperNS::GetRect(args[2]);
    bool bNeedInvalidate = LuaHelperNS::GetBool(args[3], true);
    CObjPtr->SetPos(rc, bNeedInvalidate);
    return 0;
}

int LuaContainer::Move(LuaState *L)
{
    LuaStack args(L);
    SIZE sz = LuaHelperNS::GetSize(args[2]);
    bool bNeedInvalidate = LuaHelperNS::GetBool(args[3], true);
    CObjPtr->Move(sz, bNeedInvalidate);
    return 0;
}

int LuaContainer::SetSubControlText(LuaState *L)
{
    LuaStack args(L);
    bool bRet = false;
    SDTString str1 = LuaHelperNS::GetTString(args[2]);
    SDTString str2 = LuaHelperNS::GetTString(args[3]);
    if (!str1.empty()) {
        bRet = CObjPtr->SetSubControlText(str1.c_str(), str2.c_str());
    }
    L->PushBoolean(bRet);
    return 1;
}

int LuaContainer::SetSubControlFixedHeight(LuaState *L)
{
    LuaStack args(L);
    bool bRet = false;
    SDTString str1 = LuaHelperNS::GetTString(args[2]);
    int val = LuaHelperNS::GetInt(args[3]);
    if (!str1.empty()) {
        bRet = CObjPtr->SetSubControlFixedHeight(str1.c_str(), val);
    }
    L->PushBoolean(bRet);
    return 1;
}

int LuaContainer::SetSubControlFixedWdith(LuaState *L)
{
    LuaStack args(L);
    bool bRet = false;
    SDTString str1 = LuaHelperNS::GetTString(args[2]);
    int val = LuaHelperNS::GetInt(args[3]);
    if (!str1.empty()) {
        bRet = CObjPtr->SetSubControlFixedWdith(str1.c_str(), val);
    }
    L->PushBoolean(bRet);
    return 1;
}

int LuaContainer::SetSubControlUserData(LuaState *L)
{
    LuaStack args(L);
    bool bRet = false;
    SDTString str1 = LuaHelperNS::GetTString(args[2]);
    SDTString str2 = LuaHelperNS::GetTString(args[3]);
    if (!str1.empty()) {
        bRet = CObjPtr->SetSubControlUserData(str1.c_str(), str2.c_str());
    }
    L->PushBoolean(bRet);
    return 1;
}

int LuaContainer::GetSubControlText(LuaState *L)
{
    LuaStack args(L);
    SDTString str1 = LuaHelperNS::GetTString(args[2]);
    if (!str1.empty()) {
        SDTString str = CObjPtr->GetSubControlText(str1.c_str()).GetData();
        LuaHelperNS::PushTString(L, str);
    }
    L->PushNil();
    return 1;
}

int LuaContainer::GetSubControlFixedHeight(LuaState *L)
{
    LuaStack args(L);
    SDTString str1 = LuaHelperNS::GetTString(args[2]);
    if (!str1.empty()) {
        int val = CObjPtr->GetSubControlFixedHeight(str1.c_str());
        L->PushInteger(val);
    }
    L->PushNil();
    return 1;
}

int LuaContainer::GetSubControlFixedWdith(LuaState *L)
{
    LuaStack args(L);
    SDTString str1 = LuaHelperNS::GetTString(args[2]);
    if (!str1.empty()) {
        int val = CObjPtr->GetSubControlFixedWdith(str1.c_str());
        L->PushInteger(val);
    }
    L->PushNil();
    return 1;
}

int LuaContainer::GetSubControlUserData(LuaState *L)
{
    LuaStack args(L);
    SDTString str1 = LuaHelperNS::GetTString(args[2]);
    if (!str1.empty()) {
        SDTString str = CObjPtr->GetSubControlUserData(str1.c_str()).GetData();
        LuaHelperNS::PushTString(L, str);
    }
    L->PushNil();
    return 1;
}

int LuaContainer::FindSubControl(LuaState *L)
{
    LuaStack args(L);
    SDTString str1 = LuaHelperNS::GetTString(args[2]);
    if (!str1.empty()) {
        CControlUI* ctrl = CObjPtr->FindSubControl(str1.c_str());
        LuaHelperNS::PushCObj(L, ctrl);
    }
    L->PushNil();
    return 1;
}

int LuaContainer::GetScrollPos(LuaState *L)
{
    SIZE sz = CObjPtr->GetScrollPos();
    LuaHelperNS::PushSize(L, sz);
    return 1;
}

int LuaContainer::GetScrollRange(LuaState *L)
{
    SIZE sz = CObjPtr->GetScrollRange();
    LuaHelperNS::PushSize(L, sz);
    return 1;
}

int LuaContainer::SetScrollPos(LuaState *L)
{
    LuaStack args(L);
    SIZE sz = LuaHelperNS::GetSize(args[2]);
    bool bMsg = LuaHelperNS::GetBool(args[3], true);
    CObjPtr->SetScrollPos(sz, bMsg);
    return 0;
}

void LuaContainer::SetScrollStepSize(int nSize)
{
    CObjPtr->SetScrollStepSize(nSize);
}

int LuaContainer::GetScrollStepSize()
{
    return CObjPtr->GetScrollStepSize();
}

void LuaContainer::LineUp()
{
    CObjPtr->LineUp();
}

void LuaContainer::LineDown()
{
    CObjPtr->LineDown();
}

void LuaContainer::PageUp()
{
    CObjPtr->PageUp();
}

void LuaContainer::PageDown()
{
    CObjPtr->PageDown();
}

void LuaContainer::HomeUp()
{
    CObjPtr->HomeUp();
}

void LuaContainer::EndDown()
{
    CObjPtr->EndDown();
}

void LuaContainer::LineLeft()
{
    CObjPtr->LineLeft();
}

void LuaContainer::LineRight()
{
    CObjPtr->LineRight();
}

void LuaContainer::PageLeft()
{
    CObjPtr->PageLeft();
}

void LuaContainer::PageRight()
{
    CObjPtr->PageRight();
}

void LuaContainer::HomeLeft()
{
    CObjPtr->HomeLeft();
}

void LuaContainer::EndRight()
{
    CObjPtr->EndRight();
}

int LuaContainer::EnableScrollBar(LuaState *L)
{
    LuaStack args(L);
    bool bEnableVertical = LuaHelperNS::GetBool(args[2], true);
    bool bEnableHorizontal = LuaHelperNS::GetBool(args[3], true);
    CObjPtr->EnableScrollBar(bEnableVertical, bEnableHorizontal);
    return 0;
}

int LuaContainer::GetVerticalScrollBar(LuaState *L)
{
    CScrollBarUI* ctrl = CObjPtr->GetVerticalScrollBar();
    LuaHelperNS::PushCObj(L, ctrl);
    return 1;
}

int LuaContainer::GetHorizontalScrollBar(LuaState *L)
{
     CScrollBarUI* ctrl = CObjPtr->GetHorizontalScrollBar();
    LuaHelperNS::PushCObj(L, ctrl);
    return 1;
}

SDLUA_BEGIN_REGISTER(LuaContainer, Container, Control)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, GetItemAt, GetItemAt)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, GetItemIndex, GetItemIndex)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, SetItemIndex, SetItemIndex)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, GetCount, GetCount)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, Add, Add)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, AddAt, AddAt)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, Remove, Remove)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, RemoveAt, RemoveAt)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, RemoveAll, RemoveAll)

    SDLUA_REGISTER_FUN_CPP(LuaContainer, SetVisible, SetVisible)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, SetInternVisible, SetInternVisible)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, SetEnabled, SetEnabled)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, SetMouseEnabled, SetMouseEnabled)

    SDLUA_REGISTER_FUN_LUA(LuaContainer, GetInset, GetInset)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, SetInset, SetInset)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, GetChildPadding, GetChildPadding)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, SetChildPadding, SetChildPadding)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, GetChildAlign, GetChildAlign)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, SetChildAlign, SetChildAlign)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, GetChildVAlign, GetChildVAlign)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, SetChildVAlign, SetChildVAlign)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, IsAutoDestroy, IsAutoDestroy)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, SetAutoDestroy, SetAutoDestroy)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, IsDelayedDestroy, IsDelayedDestroy)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, SetDelayedDestroy, SetDelayedDestroy)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, IsMouseChildEnabled, IsMouseChildEnabled)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, SetMouseChildEnabled, SetMouseChildEnabled)

    SDLUA_REGISTER_FUN_LUA(LuaContainer, FindSelectable, FindSelectable)
    
    SDLUA_REGISTER_FUN_LUA(LuaContainer, GetClientPos, GetClientPos)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, SetPos, SetPos)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, Move, Move)

    SDLUA_REGISTER_FUN_LUA(LuaContainer, SetSubControlText, SetSubControlText)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, SetSubControlFixedHeight, SetSubControlFixedHeight)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, SetSubControlFixedWdith, SetSubControlFixedWdith)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, SetSubControlUserData, SetSubControlUserData)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, GetSubControlText, GetSubControlText)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, GetSubControlFixedHeight, GetSubControlFixedHeight)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, GetSubControlFixedWdith, GetSubControlFixedWdith)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, GetSubControlUserData, GetSubControlUserData)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, FindSubControl, FindSubControl)

    SDLUA_REGISTER_FUN_LUA(LuaContainer, GetScrollPos, GetScrollPos)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, GetScrollRange, GetScrollRange)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, SetScrollPos, SetScrollPos)

    SDLUA_REGISTER_FUN_CPP(LuaContainer, SetScrollStepSize, SetScrollStepSize)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, GetScrollStepSize, GetScrollStepSize)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, LineUp, LineUp)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, LineDown, LineDown)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, PageUp, PageUp)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, PageDown, PageDown)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, HomeUp, HomeUp)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, EndDown, EndDown)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, LineLeft, LineLeft)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, LineRight, LineRight)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, PageLeft, PageLeft)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, PageRight, PageRight)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, HomeLeft, HomeLeft)
    SDLUA_REGISTER_FUN_CPP(LuaContainer, EndRight, EndRight)

    SDLUA_REGISTER_FUN_LUA(LuaContainer, EnableScrollBar, EnableScrollBar)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, GetVerticalScrollBar, GetVerticalScrollBar)
    SDLUA_REGISTER_FUN_LUA(LuaContainer, GetHorizontalScrollBar, GetHorizontalScrollBar)

SDLUA_END_REGISTER

SDLUA_BEGIN_UNREGISTER(LuaContainer, Container)
SDLUA_END_UNREGISTER(Container)

}

#undef CObjPtr